package File;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DemoGUI extends JFrame{

    JLabel l1 = new JLabel("语文成绩");
    JTextField t1 = new JTextField(15);
    JLabel l2 = new JLabel("数学成绩");
    JTextField t2 = new JTextField(15);
    JLabel l3 = new JLabel("平均成绩");
    JTextField t3 = new JTextField(15);
    JButton bt = new JButton("计算平均成绩");
    DemoGUI(){
        setSize(300,300);
        setLocation(400, 400);
        setLayout(new GridLayout(4,1));
        //l1.setBounds(300, 200, 100, 50);
        JPanel p1 = new JPanel();
        p1.add(l1);
        p1.add(t1);
        add(p1);
        JPanel p2 = new JPanel();
        p2.add(l2);
        p2.add(t2);
        add(p2);
        JPanel p3 = new JPanel();
        p3.add(l3);
        p3.add(t3);
        add(p3);
        JPanel p4 = new JPanel();
        bt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String g1 = t1.getText();
                String g2 = t2.getText();
                double d1 = Double.valueOf(g1);
                double d2 = Double.valueOf(g2);
                double ave = (d1+d2)/2;
                t3.setText(ave+"");
            }
        });
        p4.add(bt);
        add(p4);
    }
    public static void main(String[] args) {
        DemoGUI d = new DemoGUI();
        d.setVisible(true);
    }
}
